package com.ruoyi.web.controller.ksdH5;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.utils.Constans;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetMerchantIncreaseLimit;
import com.ruoyi.system.service.IHatchetMerchantService;
import net.sf.json.JSONObject;

/**
 * 空卡代APP 卡信息
 * 
 * @author ruoyi
 * @date 2019-06-27
 */
@Controller		 
@RequestMapping("/h5/card")
public class KsdH5Cardontroller 
{
	@Autowired
	IHatchetMerchantService merchantService;
	/**
	  * 列表
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("/list")
	 @ResponseBody
	 public Map<String, Object> list(HttpServletRequest request,HttpServletResponse response)
	 {	
		Map<String, Object> map = new HashMap<>();
		HttpSession session = request.getSession();
		Object merchant = session.getAttribute("merchant");
		Object token = session.getAttribute("token");
		//判断没有登陆状态
		if (StringUtils.isNull(merchant)|| StringUtils.isNull(token)) {
			map.put("status", "out");
		}else {
			//调用查看卡包接口
			String params="";
			String head ="v="+Constans.KSD_APP_H5_VERSION+","
						+"token="+token.toString();
			String result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/c/cardList", params,head);
			map = Ksdh5Util.getMap(result);
			if (BusinessStatus.SUCCESS.equals(map.get("status"))) {
				JSONObject jsonObject = JSONObject.fromObject(result);
				String cardArrayStr = jsonObject.get("CCList").toString();
				List<HatchetMerchantIncreaseLimit> limitList = JSONArray.parseArray(cardArrayStr,HatchetMerchantIncreaseLimit.class);
				for (HatchetMerchantIncreaseLimit limit : limitList) {
					limit.setCvn(null);
					limit.setExpdate(null);
					limit.setIdCardNumber(null);
				}
				map.put("list", limitList);
			}
		}
		return map;
	 } 
	 
	 /**
	  * 跳转卡包页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("/creditCard")
	 public String cardBag(HttpServletRequest request,HttpServletResponse response)
	 {	
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
		 
		 
		return "h5/myCard/creditCard";
	 } 
	 
	 /**
	  * 跳转储蓄卡页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("/cashCard")
	 public String cashCard(HttpServletRequest request,HttpServletResponse response)
	 {	
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
		//判断没有登陆状态
		if (!checkSession) {
			return Constans.KSD_APP_H5_LOGIN_URL;
		}
		return "h5/myCard/cashCard";
	 } 
	 
	 /**
	  * 添加信用卡
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("/addCreditCard")
	 @ResponseBody
	 public Map<String, Object> addCreditCard(HttpServletRequest request,HttpServletResponse response)
	 {	
		Map<String, Object> map = new HashMap<>();
		//判断session
		boolean checkSession = Ksdh5Util.checkSession(request, response);
		if (!checkSession) {
			map.put("status", "out");
			return map;
		}
		//判断实名认证
		 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
		 if (!checkRealName) {
				map.put("status", "noReal");
				return map;
			}
		
		HttpSession session = request.getSession();
		Object token = session.getAttribute("token");
		String cardNo = request.getParameter("cardNo");
		String bankPhone = request.getParameter("bankPhone");
		String expdate = request.getParameter("expdate");
		String cvn = request.getParameter("cvn");
		String limitMoney = request.getParameter("limitMoney");
		String billDay = request.getParameter("billDay");
		String repaymentday = request.getParameter("repaymentday");
		String num = request.getParameter("num"); //认证次数 当认证次数达到三次 后台将不做校验
		String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
		String params="bankAccount="+cardNo+"&"
				     +"cvn="+cvn+"&" 
				     +"limitMoney="+limitMoney+"&" 
				     +"billDay="+billDay+"&" 
				     +"repaymentDay="+repaymentday+"&" 
				     +"expdate="+expdate+"&" 
				     +"bankPhone="+bankPhone+"&" 
				     +"num="+num; 
		String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/c/addCXcard", params,head);
		return Ksdh5Util.getMap(result);
	 } 
	 /**
	  * 添加储蓄卡
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("/addCashCard")
	 @ResponseBody
	 public Map<String, Object> addCashCard(HttpServletRequest request,HttpServletResponse response)
	 {	
		Map<String, Object> map = new HashMap<>();
		//判断session
		boolean checkSession = Ksdh5Util.checkSession(request, response);
		if (!checkSession) {
			map.put("status", "out");
			return map;
		}
		//判断实名认证
		 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
		 if (!checkRealName) {
				map.put("status", "noReal");
				return map;
			}
		
		HttpSession session = request.getSession();
		Object token = session.getAttribute("token");
		String cardNo = request.getParameter("cardNo");
		String bankPhone = request.getParameter("bankPhone");

		String num = request.getParameter("num"); //认证次数 当认证次数达到三次 后台将不做校验
		String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
		String params="bankAccount="+cardNo+"&"
				     +"bankPhone="+bankPhone+"&" 
				     +"num="+num; 
		String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/c/addCard", params,head);
		Map<String, Object> map2 = Ksdh5Util.getMap(result);
		if (BusinessStatus.SUCCESS.equals(map2.get("status"))) {
			//更新session中用户的银行卡号
			//更新session中用户信息
			HatchetMerchant merchant = Ksdh5Util.getMerchant(request, response);
			merchant.setBankAccount(cardNo);
			session.removeAttribute("merchant");
			session.setAttribute("merchant", merchant);
		}
		return Ksdh5Util.getMap(result);
	 } 
	 
	 
	 /**
	  * 跳转更换储蓄卡页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("changeCashCard")
	 public String changeCashCard(HttpServletRequest request,HttpServletResponse response,ModelMap map) {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			HatchetMerchant merchant = Ksdh5Util.getMerchant(request, response);
			map.put("merchant",merchant);
		return "h5/myCard/changeCashCard";
	 } 
	 
	 /**
	  * 跳转信用编辑卡页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("edit")
	 public String toEditPage(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			String id = request.getParameter("id");
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			//调用查看卡包接口
			String params="";
			String head ="v="+Constans.KSD_APP_H5_VERSION+","
						+"token="+token.toString();
			String result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/c/cardList", params,head);
			
			
			 Map<String, Object> map = Ksdh5Util.getMap(result);
			if (BusinessStatus.SUCCESS.equals(map.get("status"))) {
				JSONObject jsonObject = JSONObject.fromObject(result);
				String cardArrayStr = jsonObject.get("CCList").toString();
				List<HatchetMerchantIncreaseLimit> limitList = JSONArray.parseArray(cardArrayStr,HatchetMerchantIncreaseLimit.class);
				for (HatchetMerchantIncreaseLimit limit : limitList) {
					if(id.equals(limit.getID())) {
						modelMap.put("limit", limit);
						break;
					}
				}
			}
			
			//String cardNo = request.getParameter("cardNo");
			//查询用户的卡信息
		return "h5/myCard/edit";
	 } 
	 
	 
	 /**
	  * 修改信用卡信息
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("/editCreditCard")
	 @ResponseBody
	 public Map<String, Object> editCreditCard(HttpServletRequest request,HttpServletResponse response)
	 {	
		Map<String, Object> map = new HashMap<>();
		//判断session
		boolean checkSession = Ksdh5Util.checkSession(request, response);
		if (!checkSession) {
			map.put("status", "out");
			return map;
		}
		//判断实名认证
		 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
		 if (!checkRealName) {
				map.put("status", "noReal");
				return map;
			}
		
		HttpSession session = request.getSession();
		Object token = session.getAttribute("token");
		String bankPhone = request.getParameter("bankPhone");
		String id = request.getParameter("id");
		String expdate = request.getParameter("expdate");
		String cvn = request.getParameter("cvn");
		String limitMoney = request.getParameter("limitMoney");
		String billDay = request.getParameter("billDay");
		String repaymentDay = request.getParameter("repaymentDay");
		String num = request.getParameter("num"); //认证次数 当认证次数达到三次 后台将不做校验
		String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
		String params="id="+id+"&"
				     +"cvn="+cvn+"&" 
				     +"limitMoney="+limitMoney+"&" 
				     +"billDay="+billDay+"&" 
				     +"repaymentDay="+repaymentDay+"&" 
				     +"expdate="+expdate+"&" 
				     +"bankPhone="+bankPhone+"&" 
				     +"num="+num; 
		String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/c/updateCreditCard", params,head);
		return Ksdh5Util.getMap(result);
	 } 
	 
		 /**
		  * 修改信用卡信息
		  * @param request
		  * @param response
		  * @return
		  */
		 @PostMapping("/unBind")
		 @ResponseBody
		 public Map<String, Object> unBind(HttpServletRequest request,HttpServletResponse response)
		 {	
			Map<String, Object> map = new HashMap<>();
			//判断session
			boolean checkSession = Ksdh5Util.checkSession(request, response);
			if (!checkSession) {
				map.put("status", "out");
				return map;
			}
			//判断实名认证
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
					map.put("status", "noReal");
					return map;
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			String cardNo = request.getParameter("cardNo");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
						+ "token="+token.toString();
			String params="cardNo="+cardNo;
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/c/UnbundlingCard", params,head);
			return Ksdh5Util.getMap(result);
		 } 
		 
	 
}